﻿using Microsoft.AspNetCore.Mvc;
using NetCoreProject.Api.Filters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Filters;

namespace NetCoreProject.Api.Common
{
    //接口响应参数
    [ModelValidation]
    public class ReponseResult: ControllerBase
    {
        public ActionResult CreateRespone(Action<ResponseParamter> Func)
        {
             return  CreateRespone(response => {
                 Func.Invoke(response);
                  return Content(response.ToString());
            });
        }
        public ActionResult CreateRespone(Func<ResponseParamter, ActionResult> Func)
        {
            ResponseParamter response = new ResponseParamter();
            var result = Func.Invoke(response);
            return result;
        }

    }

    public class ResponseParamter
    {
        public int Code { get; set; }

        public string Msg { get; set; }
        public object Data { get; set; }

        public DateTime OperatTime { get { return DateTime.Now; } }
        public bool IsSuccess { get { return Msg.Contains("成功"); } }



        public override string ToString()
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            //settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            settings.DateFormatString = "yyyy/MM/dd HH:mm:ss";
            return JsonConvert.SerializeObject(this, settings);
        }
    }
}
